package com.Xht.www.servlet;

//后端响应处理用户提交充值申请的请求

import com.Xht.www.dao.UserWalletBalanceDAO;
import com.Xht.www.po.UserWalletBalancePO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/submitRecharge")
public class SubmitRechargeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取前端传递的参数
            int userId = Integer.parseInt(request.getParameter("userId"));
            UserWalletBalanceDAO dao = new UserWalletBalanceDAO();

            // 检查用户是否有审核中的充值记录
            boolean hasPending = dao.hasPendingRecharge(userId);
            String confirmContinue = request.getParameter("confirmContinue");

            if (hasPending && !"true".equals(confirmContinue)) {
                response.getWriter().write("您上条充值仍在审核中，继续充值默认放弃上条充值，请问是否继续？");
                return;
            }

            BigDecimal topUpAmount = new BigDecimal(request.getParameter("topUpAmount"));
            String rechargeAuditStatus = request.getParameter("rechargeAuditStatus");
            String rechargeReason = request.getParameter("rechargeReason");

            // 检查用户钱包记录是否存在
            UserWalletBalancePO walletBalance = dao.getUserByUserId(userId);

            if (walletBalance == null) {
                // 如果记录不存在，创建新的钱包记录
                walletBalance = new UserWalletBalancePO();
                walletBalance.setUserId(userId);
                // 将 walletId 设置为和 userId 一样
                walletBalance.setWalletId(userId);
                walletBalance.setTopUpAmount(topUpAmount);
                walletBalance.setRechargeAuditStatus(rechargeAuditStatus);
                walletBalance.setRechargeReason(rechargeReason);
                // 确保各金额字段初始为0
                walletBalance.setTotalAmount(BigDecimal.ZERO);
                walletBalance.setRechargeAmount(BigDecimal.ZERO);
                walletBalance.setConsumptionAmount(BigDecimal.ZERO);

                // 插入新的钱包记录
                boolean insertSuccess = dao.insertUserWalletBalance(walletBalance);
                if (!insertSuccess) {
                    response.getWriter().write("创建用户钱包记录失败，请重试！");
                    return;
                }
            } else {
                // 如果记录存在，更新钱包相关信息
                walletBalance.setTopUpAmount(topUpAmount);
                walletBalance.setRechargeAuditStatus(rechargeAuditStatus);
                walletBalance.setRechargeReason(rechargeReason);

                // 调用 DAO 进行更新操作
                boolean updateSuccess = dao.updateUserWalletBalance(walletBalance);
                if (!updateSuccess) {
                    response.getWriter().write("更新用户钱包信息失败，请重试！");
                    return;
                }
            }

            response.getWriter().write("充值成功！");
        } catch (NumberFormatException e) {
            response.getWriter().write("参数格式错误，请检查输入！");
            e.printStackTrace();
        } catch (Exception e) {
            response.getWriter().write("服务器内部错误，请稍后再试！");
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        UserWalletBalanceDAO dao = new UserWalletBalanceDAO();
        boolean hasPending = dao.hasPendingRecharge(userId);
        response.setContentType("application/json");
        response.getWriter().write(String.valueOf(hasPending));
    }
}
